package com.example.zhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhxy.pojo.Admin;
import com.example.zhxy.service.AdminService;
import com.example.zhxy.util.JwtHelper;
import com.example.zhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * CJ
 */
@Slf4j
@Api(tags = "管理员控制层")
@RestController
@RequestMapping("/sms/adminController")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @ApiOperation(" 分页 ")
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result page(@PathVariable Integer pageNo, @PathVariable Integer pageSize, String adminName){
        Page<Admin> page=new Page<>(pageNo,pageSize);
        IPage page1=adminService.IPage(page,adminName);
        return Result.ok(page1);
    }
    @ApiOperation("新增和修改")
    @PostMapping("/saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(@RequestBody Admin admin){
        adminService.saveOrUpdate(admin);
        return Result.ok();
    }
    @ApiOperation("删除功能，不可删除自身")
    @DeleteMapping("/deleteAdmin")
    public Result remove(@RequestBody List<Integer> list, @RequestHeader("token") String token){
        log.info(token);
        Integer userId = JwtHelper.getUserId(token).intValue();
        log.info(userId.toString());
        adminService.deleteAdmin(list,userId);
        return Result.ok();

    }
}
